package com.admin.action;

import com.admin.service.SoftwareExamineService;
import com.data.util.CheckDataUtils;
import com.data.util.FileResumeUtils;
import com.data.util.FinalStringUtils;
import com.data.util.ImageLookUtils;
import com.user.bean.AppIcon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 信息审核，身份认证审核，apk文件审核，下线申请等相关操作
 */
@Controller
@RequestMapping("massageExamineAction/admin/")
public class SoftwareExamineAction {

    @Autowired
    private SoftwareExamineService softwareExamineService;

    /**
     * 信息审核列表浏览
     *
     * @return
     */
    @GetMapping(value = "/list")
    public String inputMassageListAction(HttpServletRequest request, int type, String fid) {
        List<?> list = softwareExamineService.findSoftwareService(type, fid);
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/apply/applySoftware";
    }

    /**
     * 查看软件的小图标中图标
     *
     * @param response
     * @param rid      软件的id
     * @param type     图片的类型
     * @throws IOException
     */
    @GetMapping("lookIcon")
    public void readSoftwareImageAction(HttpServletResponse response, String rid, String type) {
        if (!CheckDataUtils.stringUtilsID(rid, type))
            return;
        rid = rid.trim();
        type = type.trim();
        byte[] inputStream = softwareExamineService.lookImageService(rid, type);
        ImageLookUtils.responseImage(inputStream, response);

    }

    /**
     * 进入到查看应用截图
     *
     * @param id
     * @param type
     * @param request
     * @return
     */
    @GetMapping("InputBigIcon")
    public String InputBigIconAction(String id, String type, HttpServletRequest request) {
        List<AppIcon> list = softwareExamineService.findSoftwareIconService(id, type);
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/apply/applySoftIcon";
    }

    /***
     * 查看应用截图
     * @param id
     * @param name
     * @param response
     * @throws IOException
     */
    @GetMapping("lookBigIcon")
    public void lookBigIconAction(String id, String name, HttpServletResponse response) {
        byte[] inputStream = ImageLookUtils.lookImageNameUtils(id, name);
        ImageLookUtils.responseImage(inputStream, response);

    }

    /**
     * 软件下载实现
     *
     * @param md5
     * @return
     * @throws IOException
     */
    @RequestMapping("downloadApk")
    public void downloadApkAction(String md5, HttpServletRequest request, HttpServletResponse response) {
        String fileName = md5 + ".apk";
        //软件新的路径
        String filePath = FinalStringUtils.APKPATH + "apk/" + fileName;
        try {
            FileResumeUtils.downloadApk(fileName, filePath, request, response);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件下载出错");
        }

    }


    /**
     * 传入信息认证结果，通过，或者拒绝
     *
     * @param id       软件id
     * @param state    审核状态
     * @param contents 提示内容
     * @return
     */


    @RequestMapping("/passApk")
    @ResponseBody
    public String softwarePassAction(String id, int state, String contents) {
        boolean bool = false;
        if (CheckDataUtils.stringUtils(id)) {
            //通过申请
            if (state == 1) {
                bool = softwareExamineService.softwarePassService(id, state);
                // TODO://发送通知邮件，用于通知软件是否通过审核
                //为通过审核
            } else if (state == -1) {
                bool = softwareExamineService.softwareRefuseService(id, state, contents);
            }

        }


        return String.valueOf(bool);

    }

}
